package com.jinxinol.basic.system.web;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import com.jinxinol.basic.system.domain.SysRight;
import com.jinxinol.basic.system.domain.SysRole;
import com.jinxinol.basic.system.domain.SysRoleright;
import com.jinxinol.basic.system.domain.SysSubsystem;
import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.basic.system.service.ISysRightService;
import com.jinxinol.basic.system.service.ISysRoleService;
import com.jinxinol.basic.system.service.ISysRolerightService;
import com.jinxinol.basic.system.service.ISysSubsystemService;
import com.jinxinol.basic.system.service.ISysUserService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;
import com.jinxinol.core.common.PageModel;
import com.jinxinol.core.util.encryption.MD5;

/**
 * 
 * @company Jinxin Computer Corp.
 * @author Conner
 * @email 764828388@qq.com
 * @description 角色管理Action
 * @date 2012-9-5
 */
public class RoleAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log logger = ContextHelper.getLogger(RoleAction.class);
	
	private SysRole role = new SysRole();
	private SysRight right = new SysRight();
	private SysSubsystem subSystem = new SysSubsystem();
	private SysRoleright sysRoleRight = new SysRoleright();
	private PageModel<SysRole> pm = new PageModel<SysRole>();
	private List<SysRole> list;
	private List<SysRight> list_right;
	private List<SysSubsystem> listSubSystem;
	private List<SysRoleright> listRoleright;
	private String idStr;
	private String type;
	private Integer id;  //角色ID
	private Integer right_id;  //权限id
	
	private int i = 0;
	private int j = 0;
	
	@Resource 
	ISysRoleService roleService;
	
	@Resource 
	ISysRightService rightService;
	
	@Resource
	ISysSubsystemService sysSubsystemService;
	
	@Resource
	ISysRolerightService sysRolerightService;
	
	public RoleAction(){}
	
	/**
	 * 添加角色
	 * */
	public String add(){
		try {
			role.setRname(role.getRname().trim());
			role.setRemark(role.getRemark().trim());
			roleService.insert(role);
			return "addSuccess";
		} catch (Exception e) {
			logger.error("新增角色"+role.getRname() +"失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 查询所有子系统信息
	 */
	public String findAllSubSys(){
		try {
			listSubSystem = sysSubsystemService.find(subSystem);  //查询所有子系统信息
			return "findAllSubSysSuccess";
		} catch (Exception e) {
			logger.error("查询所有子系统信息失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 删除角色
	 * */
	public String delete(){
		String[] arr = null;
		try {
			if(!idStr.equals("")){
				arr = idStr.split(",");
				for(int i = 0; i < arr.length ; i++){
					//删除角色前,先判断角色是否已经授权,若已经授权,则不能删除.
					sysRoleRight = new SysRoleright();
					sysRoleRight.setRole_id(Integer.parseInt(arr[i]));
					listRoleright = sysRolerightService.find(sysRoleRight);
					logger.info("listRoleright.size()="+listRoleright.size());
					if(listRoleright.size() > 0){
						logger.error("角色不能删除，角色已经授权！", null);
					}else if(listRoleright.size() == 0){
						role = new SysRole();
						role.setId(Integer.parseInt(arr[i]));
						roleService.delete(role);
						logger.info("删除角色成功！");
					}
				}
			}
			return "delSuccess";
		} catch (Exception e) {
			logger.error("删除角色出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 修改角色信息
	 * */
	public String update(){
		try {
			role.setRname(role.getRname().trim());
			role.setRemark(role.getRemark().trim());
			roleService.update(role);
			return "updSuccess";
		} catch (Exception e) {
			logger.error("修改角色"+role.getRname()+"出现异常！", e);
			return ERROR;
		}
	}
	
	/**
	 * 修改角色状态
	 * */
	public void updateRoleState(){
		try {
			roleService.update(role);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("修改角色"+role.getRname()+"状态出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
			jsonObject.put(RESULT_MSG, "改变角色状态失败，请联系管理员！");
		}
		this.pushJsonResult();
	}
	
	/**
	 * 按ID查询角色
	 * */
	public String queryByRid(){
		try {
			role = roleService.findById(role.getId());
			listSubSystem = sysSubsystemService.find(subSystem);  //查询所有子系统信息
			return "qurSuccess";
		} catch (Exception e) {
			logger.error("按ID查询角色出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 按条件查询角色
	 * */
	public String selectRole(){
		String subSysName = null;
		try {
			listSubSystem = sysSubsystemService.find(subSystem);  //查询所有子系统信息
			pm = roleService.findOnPage(role, pm);
			//设置子系统名称
			if(pm.getList() != null && pm.getList().size() > 0 &&
					listSubSystem != null && listSubSystem.size() > 0){
				for(i = 0 ; i < pm.getList().size() ; i++){
					for(j = 0 ; j < listSubSystem.size() ; j++){
						if(pm.getList().get(i).getSub_id() == listSubSystem.get(j).getId()){
							subSysName = listSubSystem.get(j).getName();
							pm.getList().get(i).setSubSysName(subSysName);
						}
					}
				}
			}
			return "list";
		} catch (Exception e) {
			logger.error("按条件查询角色出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 根据子系统id, 查询所有子系统角色
	 * */
	public String seSubRoBySubId(){
		try {
			list = roleService.find(role);  //查询所有子系统角色
			subSystem = sysSubsystemService.findById(role.getSub_id());
			return "list2";
		} catch (Exception e) {
			logger.error("按条件查询角色出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 授权
	 */
	public String grant(){
		String[] arr = null;
		try {
			if(!idStr.equals("")){
				arr = idStr.split(",");
				//添加授权以前,先删除已有的权限
				for(int i = 0; i < arr.length ; i++){
					sysRoleRight = new SysRoleright();
					if(type.equals("grtRight2Role")){
						sysRoleRight.setRole_id(Integer.parseInt(arr[i]));
					}else if(type.equals("roleGrant")){
						sysRoleRight.setRole_id(id);
					}
					sysRolerightService.delete(sysRoleRight);
				}
				for(int i = 0; i < arr.length ; i++){
					sysRoleRight = new SysRoleright();
					if(type.equals("grtRight2Role")){
						sysRoleRight.setRight_id(right_id);
						sysRoleRight.setRole_id(Integer.parseInt(arr[i]));
					}else if(type.equals("roleGrant")){
						sysRoleRight.setRight_id(Integer.parseInt(arr[i]));
						sysRoleRight.setRole_id(id);
					}
					//添加前,先判断是否已经存在此项,若存在,则不重复添加
					if(sysRolerightService.find(sysRoleRight).size() == 0){
						sysRolerightService.insert(sysRoleRight);
					}else{
						if(type.equals("grtRight2Role")){
							logger.info("权限(菜单)id为:" + right_id + ", 角色id为:" + arr[i] + "的项已存在！");
						}else if(type.equals("roleGrant")){
							logger.info("权限(菜单)id为:" + arr[i] + ", 角色id为:" + id + "的项已存在！");
						}
					}
				}
				logger.info("角色授权成功！");
			}else if(idStr.equals("")){
				//不授权
				sysRoleRight = new SysRoleright();
				if(type.equals("roleGrant")){
					sysRoleRight.setRole_id(id);
					sysRolerightService.delete(sysRoleRight);
				}
				logger.info("已删除角色的所有授权！");
			}
			if(type.equals("grtRight2Role")){
				return "grantSuccess";
			}else{
				return "grantSuccess2";
			}
		} catch (Exception e) {
			logger.error("角色授权出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 加载角色权限
	 */
	public void findRoleRight(){
		try {
			listRoleright = sysRolerightService.find(sysRoleRight);
			//做这个处理的原因是:zTree在勾选节点的时候,一旦勾选父节点,则全部勾选子节点;
			//为了只勾选叶子节点,所以后台做了判定,把角色权限列表中权限是叶子节点的,值设为1.
			for(i = 0 ; i < listRoleright.size() ; i++){
				right = new SysRight();
				right.setParent_id(listRoleright.get(i).getRight_id());
				list_right = rightService.find(right);
				if(list_right.size() == 0){
					listRoleright.get(i).setIs_leaf(1);
				}
			}
			jsonObject.put(RESULT_CODE, SUCCESS);
			jsonObject.put("listRoleright", listRoleright);
		} catch (Exception e) {
			logger.error("加载角色权限信息失败！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		
		this.pushJsonResult();
	}
	
	//getter & setter
	public SysRole getRole() {
		return role;
	}
	
	public void setRole(SysRole role) {
		this.role = role;
	}
	
	public SysRight getRight() {
		return right;
	}

	public void setRight(SysRight right) {
		this.right = right;
	}

	public SysSubsystem getSubSystem() {
		return subSystem;
	}

	public void setSubSystem(SysSubsystem subSystem) {
		this.subSystem = subSystem;
	}

	public PageModel<SysRole> getPm() {
		return pm;
	}
	
	public void setPm(PageModel<SysRole> pm) {
		this.pm = pm;
	}
	
	public List<SysRole> getList() {
		return list;
	}
	
	public void setList(List<SysRole> list) {
		this.list = list;
	}

	public List<SysSubsystem> getListSubSystem() {
		return listSubSystem;
	}

	public void setListSubSystem(List<SysSubsystem> listSubSystem) {
		this.listSubSystem = listSubSystem;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getIdStr() {
		return idStr;
	}

	public void setIdStr(String idStr) {
		this.idStr = idStr;
	}

	public Integer getRight_id() {
		return right_id;
	}

	public void setRight_id(Integer right_id) {
		this.right_id = right_id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<SysRoleright> getListRoleright() {
		return listRoleright;
	}

	public void setListRoleright(List<SysRoleright> listRoleright) {
		this.listRoleright = listRoleright;
	}

	public SysRoleright getSysRoleRight() {
		return sysRoleRight;
	}

	public void setSysRoleRight(SysRoleright sysRoleRight) {
		this.sysRoleRight = sysRoleRight;
	}
	
}
